﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;
using System.Drawing;
using System.IO;


namespace School.Pages.Teaching
{
    public partial class TE_Topic_Edit : System.Web.UI.Page
    {
        private static SchoolEntities db = new SchoolEntities();
        public string id;

        protected void Page_Load(object sender, EventArgs e)
        {
            lblattach.Visible = false;
            lbltst.Visible = false;
            if (!this.Page.IsPostBack)
            {
                FillControls();
                id = Request.QueryString["Id"].ToString();
                Hd_Id.Value = id.ToString();
                if (!string.IsNullOrEmpty(id))
                {
                    long T_ID = long.Parse(id);
                    var q = from a in db.TE_Topic
                            where a.Id == T_ID
                            select new
                            {
                                Grad_name = a.TE_Course.TE_Subject.ORG_Grade.Name,
                                Grad = a.TE_Course.TE_Subject.ORG_Grade.Id,
                                Sub_name = a.TE_Course.TE_Subject.Subject_Name,
                                Course_Name = a.TE_Course.Name,
                                a.Name,
                                a.Id,
                                Campus = a.TE_Course.TE_Subject.ORG_Grade.ORG_Stage.ORG_Certificate.ORG_Campus.Id,
                                Certifict = a.TE_Course.TE_Subject.ORG_Grade.ORG_Stage.ORG_Certificate.Id,
                                satage = a.TE_Course.TE_Subject.ORG_Grade.ORG_Stage.Id,
                                subject = a.TE_Course.TE_Subject.Id,
                                Course = a.TE_Course.Id,
                                content = a.contents

                            };
                    var classe = from c in db.TE_Topic_Classes where c.Topic_Id == T_ID select c;
                  
                    var file = School.Teaching.Teaching.Get_Topic_Attach(Convert.ToInt32(id));
                    ddlCampus.SelectedValue = q.FirstOrDefault().Campus.ToString();
                    ddlCertificate.DataSource = School.Uniform.PayOrder.GetCertificateByCamp(int.Parse(ddlCampus.SelectedValue));
                    ddlBind(ddlCertificate);
                    ddlCertificate.SelectedValue = q.FirstOrDefault().Certifict.ToString();
                    ddlStadge.DataSource = School.Uniform.PayOrder.GetStadgeByCertificate(int.Parse(ddlCertificate.SelectedValue));
                    ddlBind(ddlStadge);
                    ddlStadge.SelectedValue = q.FirstOrDefault().satage.ToString();
                    ddlGrade.DataSource = School.Uniform.PayOrder.GetGradeByStadge(int.Parse(ddlStadge.SelectedValue));
                    ddlBind(ddlGrade);
                    ddlGrade.SelectedValue = q.FirstOrDefault().Grad.ToString();

                    ///
                    LbClasses.DataSource = School.Teaching.Teaching.Get_Class_By_Grad(Convert.ToInt32(ddlGrade.SelectedValue));
                    LbClasses.Items.Clear();
                    LbClasses.Items.Insert(0, new ListItem("اختيار", "0"));

                    LbClasses.DataBind();
                    LbClasses.SelectedValue = "0";


                    foreach (var item in classe)
                    {
                        foreach (ListItem list in LbClasses.Items)
                        {
                            if (list.Value == item.Class_Id.ToString() && list.Value != "0")
                            {
                                list.Selected = true;

                            }
                            else if (list.Value == "0")
                                list.Selected = false;
                        }

                    }

                    ///
                    ddlBind(ddlsubject, School.Teaching.Teaching.Get_Subject_By_Grad(Convert.ToInt32(ddlGrade.SelectedValue)));
                    ddlsubject.SelectedValue = q.FirstOrDefault().subject.ToString();
                    ddlBind(ddlCourse, School.Teaching.Teaching.Get_Course_By_Subject(Convert.ToInt32(ddlsubject.SelectedValue)));
                    ddlCourse.SelectedValue = q.FirstOrDefault().Course.ToString();
                    EDcontent.Content = q.FirstOrDefault().content;
                    txtName.Text = q.FirstOrDefault().Name;
                    gvAttachBind();

                }
            }
            lbltst.Visible = false;
        }
        private void gvAttachBind()
        {
            gvAttach.DataSource = School.Teaching.Teaching.Get_Topic_Attach(int.Parse(Hd_Id.Value.ToString()));
            gvAttach.DataBind();
        }
        private void FillControls()
        {
            ddlCampus.DataSource = School.Uniform.PayOrder.AllCampus();
            ddlCampus.DataBind();
        }
        private void ddlBind(DropDownList ddl)
        {
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }
        protected void ddlCertificate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCertificate.SelectedValue != "0")
            {
                ddlStadge.DataSource = School.Uniform.PayOrder.GetStadgeByCertificate(int.Parse(ddlCertificate.SelectedValue));
                ddlBind(ddlStadge);
            }
        }
        protected void ddlStadge_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlStadge.SelectedValue != "0")
            {
                ddlGrade.DataSource = School.Uniform.PayOrder.GetGradeByStadge(int.Parse(ddlStadge.SelectedValue));
                ddlBind(ddlGrade);
            }
        }
        protected void ddlCampus_TextChanged(object sender, EventArgs e)
        {
            if (ddlCampus.SelectedValue != "0")
            {
                ddlCertificate.DataSource = School.Uniform.PayOrder.GetCertificateByCamp(int.Parse(ddlCampus.SelectedValue));
                ddlBind(ddlCertificate);
            }
        }
        private void ddlBind(DropDownList ddl, object o)
        {
            ddl.DataSource = o;
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }
        protected void ddlGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlGrade.SelectedIndex > 0)
            {
                ddlBind(ddlsubject, School.Teaching.Teaching.Get_Subject_By_Grad(Convert.ToInt32(ddlGrade.SelectedValue)));
                LbClasses.DataSource = School.Teaching.Teaching.Get_Class_By_Grad(Convert.ToInt32(ddlGrade.SelectedValue));
                LbClasses.Items.Clear();
                LbClasses.Items.Insert(0, new ListItem("اختيار", "0"));

                LbClasses.DataBind();
                LbClasses.SelectedValue = "0";
            }
        }
        protected void ddlsubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlsubject.SelectedValue != "0")
            {
                ddlBind(ddlCourse, School.Teaching.Teaching.Get_Course_By_Subject(Convert.ToInt32(ddlsubject.SelectedValue)));
            }
        }
        protected void ddlCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            long id = long.Parse(Hd_Id.Value.ToString());
            var classe = (from c in db.TE_Topic_Classes where c.Topic_Id == id select c).ToList();
            bool re = School.Teaching.Teaching.UpdateTopic(Convert.ToInt32(Hd_Id.Value.ToString()), Convert.ToInt32(ddlCourse.SelectedValue), txtName.Text, EDcontent.Content);
            foreach (var d in classe)
            {
                re = School.Teaching.Teaching.Delete_Topic_Class(Convert.ToInt32(d.Topic_Id));
            }

            if (re == true)
                foreach (ListItem item in LbClasses.Items)
                {
                    if (item.Selected && item.Value != "0")
                    {
                        re = School.Teaching.Teaching.Add_Topic_Classes(Convert.ToInt32(re), Convert.ToInt32(item.Value.ToString()));
                    }
                }

            if (re == true)
            {
                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تم التحديث بنجاح";
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية التحديث..حاول مرة اخري";
            }
            lbltst.Visible = true;

        }
        protected void gvAttach_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lkdelete = (LinkButton)e.Row.FindControl("lkdelete");
                lkdelete.Attributes.Add("onclick", "javascript:return " +
                "confirm('هل أنت متأكد من عملية الحذف')");
            }
        }
        protected void gvAttach_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int Id = Convert.ToInt32(gvAttach.DataKeys[e.RowIndex].Value.ToString());
            bool re = School.Teaching.Teaching.DeleteTopic_Attach(Id);
            if (re == true)
            {
                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تم الحذف بنجاح";
                gvAttachBind();
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية الحذف..حاول مرة اخري";
            }
            lbltst.Visible = true;
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                try
                {
                    string filename = Path.GetFileName(FileUpload1.FileName);
                    FileUpload1.SaveAs(Server.MapPath("~/Attach/") + filename);
                    School.Teaching.Teaching.Add_Topic_Attach(Convert.ToInt32(Hd_Id.Value.ToString()), filename);
                    lblattach.ForeColor = Color.Green;
                    lblattach.Text = "تمت الاضافة بنجاح";
                    gvAttachBind();
                }
                catch (Exception ex)
                {
                    lblattach.ForeColor = Color.Red;
                    lblattach.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                }
                lblattach.Visible = true;
            }

        }

        protected void gvAttach_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAttach.PageIndex = e.NewPageIndex;
            gvAttachBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Server.Transfer("~/Teaching/TE_Topic.aspx");
            //Response.Redirect
        }
    }
}
